﻿using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using UniMana.Data;
using UniMana.Utils.Data;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
using UniMana.Utils.Configs;

namespace UniMana.Win.Leadership
{
    public partial class FrmLog_ForceCanceled_Censorship_ : DevExpress.XtraEditors.XtraForm
    {
        private DataTable m_tblTable = new DataTable();
        private DBMain m_data = new DBMain();
        Log_ForceCanceledInfo force;
        int m_iLog_ForceCanceledKey = 0;
        bool m_bPermitValue;
        bool m_bRefuseValue;

        public FrmLog_ForceCanceled_Censorship_()
        {
            if (!SysUtils.CheckAccessByRule(RuleConfig.XLQCHV_BTH))
            {
                return;
            }
            InitializeComponent();
        }

        private void FrmLog_ForceCanceled_Censorship__Load(object sender, EventArgs e)
        {
            GetData();
        }

        /// <summary>
        /// Lấy dữ liệu
        /// </summary>

        private void GetData()
        {
            m_tblTable = new DataTable();
            m_tblTable = m_data.m_DCReadOnly.Log_ForceCanceleds.GetAllData().ToDataTable();
            if (m_tblTable.Rows.Count > 0)
                gctView.DataSource = m_tblTable;

            lueTraining_TrainingProgram.GetData(m_data.m_DCReadOnly.Training_TrainingPrograms.GetData(), "Training_TrainingProgramKey", "Name");
            luePersonnel_Personnel.GetData(m_data.m_DCReadOnly.Personnel_Personnels.GetPersonnelKeyIDLastNameName(), "Personnel_PersonnelKey", "FullName");
            rluTraining_Student.GetData(m_data.m_DCReadOnly.Training_Students.GetData(), "Training_StudentKey", "Name");
        }

        /// <summary>
        /// Đóng form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnClose_Click(object sender, EventArgs e)
        {
            DialogResult result = XtraMessageBox.Show("Bạn có muốn thoát", "Thông báo", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
                this.Close();
        }

        /// <summary>
        /// Chọn mẫu tin
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void gctView_Click(object sender, EventArgs e)
        {
            if (!grvView.IsFilterRow(grvView.FocusedRowHandle))
            {
                try
                {
                    m_iLog_ForceCanceledKey = int.Parse(grvView.GetFocusedRowCellValue("Log_ForceCanceledKey").ToString());
                    ShowInformation(m_iLog_ForceCanceledKey);
                }
                catch { }
            }
        }

        /// <summary>
        /// Hiển thị thông tin chi tiết mẫu tin chọn
        /// </summary>
        /// <param name="Log_ForceCanceledKey"></param>

        private void ShowInformation(int Log_ForceCanceledKey)
        {
            force = new Log_ForceCanceledInfo(Log_ForceCanceledKey);
            DateTime datime = new DateTime();
            datime = (DateTime)(force.Properties.RequireTime);
            lueTraining_TrainingProgram.EditValue = force.Properties.Training_TrainingProgramKey;
            luePersonnel_Personnel.EditValue = Guid.Parse(m_data.m_DCReadOnly.System_Users.ReturnPersonnalWithUserKey(force.Properties.RequireUser.ToString()));
            txtDatetime.Text = datime.Day + "/" + datime.Month + "/" + datime.Year + " " + datime.Hour + ":" + datime.Minute + ":" + datime.Second;
            rtbReason.Text = force.Properties.Reasons;
            try
            {
                rtbFeedback.Text = force.Properties.Feedback;
            }
            catch { }
            if (force.Properties.Status == true)
                rtbFeedback.Enabled = false;
            else
                rtbFeedback.Enabled = true;
        }

        /// <summary>
        /// Tô màu các mã phiếu chưa duyệt
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void grvView_RowStyle(object sender, RowStyleEventArgs e)
        {
            GridView View = sender as GridView;
            if (!View.IsFilterRow(e.RowHandle))
            {
                bool permit = Convert.ToBoolean(View.GetRowCellValue(e.RowHandle, View.Columns["Permit"]));
                bool refuse = Convert.ToBoolean(View.GetRowCellValue(e.RowHandle, View.Columns["Refuse"]));
                if ((permit == false) && (refuse == false))
                    e.Appearance.BackColor = Color.Silver;
            }
        }

        /// <summary>
        /// Phê duyệt phiếu buộc thôi học
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void grvView_ShowingEditor(object sender, CancelEventArgs e)
        {
            if (this.grvView.FocusedColumn == gclPermit)
            {
                m_iLog_ForceCanceledKey = int.Parse(this.grvView.GetRowCellValue(this.grvView.FocusedRowHandle, "Log_ForceCanceledKey").ToString());
                ShowInformation(m_iLog_ForceCanceledKey);
                force = new Log_ForceCanceledInfo(m_iLog_ForceCanceledKey);
                if (force.Properties.Log_ForceCanceledKey > 0)
                {
                    if (force.Properties.Status.Value == false)
                    {
                        DialogResult result = XtraMessageBox.Show("Bạn muốn phê duyệt phiếu " + m_iLog_ForceCanceledKey, "Thông báo", MessageBoxButtons.YesNo);
                        if (result == DialogResult.Yes)
                        {
                            m_bPermitValue = (bool)this.grvView.GetFocusedRowCellValue(gclPermit);
                            this.grvView.SetFocusedRowCellValue(gclPermit, !m_bPermitValue);

                            // Cập nhật trạng thái sinh viên
                            Training_Student_ClassInfo studentclass = new Training_Student_ClassInfo(force.Properties.Training_StudentKey.ToString(), int.Parse(force.Properties.Training_TrainingProgramKey.ToString()), true);
                            studentclass.Properties.Catalogue_StatusKey = 14;
                            studentclass.Save();

                            // Ghi nhận phê duyệt
                            force.Properties.Permit = true;
                            force.Properties.Status = true;
                            if (rtbFeedback.Text != "")
                                force.Properties.Feedback = rtbFeedback.Text;
                            force.Properties.CensorshipUser = Utils.LoginInfo.GblLoginInfo.sysLoginUserKey;
                            force.Properties.CensorshipTime = SysDateTime.Now;
                            force.Save();

                            XtraMessageBox.Show("Đã phê duyệt phiếu " + m_iLog_ForceCanceledKey, "Thông báo");
                            e.Cancel = true;
                            return;
                        }
                        else
                        {
                            e.Cancel = true;
                            return;
                        }
                    }
                    else
                    {
                        XtraMessageBox.Show("Phiếu " + m_iLog_ForceCanceledKey + " đã duyệt. Không thể chỉnh sửa");
                        e.Cancel = true;
                        return;
                    }
                }
            }
            if (this.grvView.FocusedColumn == gclRefuse)
            {
                m_iLog_ForceCanceledKey = int.Parse(this.grvView.GetRowCellValue(this.grvView.FocusedRowHandle, "Log_ForceCanceledKey").ToString());
                ShowInformation(m_iLog_ForceCanceledKey);
                force = new Log_ForceCanceledInfo(m_iLog_ForceCanceledKey);
                if (force.Properties.Log_ForceCanceledKey > 0)
                {
                    if (force.Properties.Status.Value == false)
                    {
                        if (rtbFeedback.Text != "")
                        {
                            DialogResult result = XtraMessageBox.Show("Bạn không muốn phê duyệt phiếu " + m_iLog_ForceCanceledKey, "Thông báo", MessageBoxButtons.YesNo);
                            if (result == DialogResult.Yes)
                            {
                                m_bRefuseValue = (bool)this.grvView.GetFocusedRowCellValue(gclRefuse);
                                this.grvView.SetFocusedRowCellValue(gclRefuse, !m_bRefuseValue);

                                // Ghi nhận phê duyệt
                                if (rtbFeedback.Text != "")
                                    force.Properties.Feedback = rtbFeedback.Text;
                                force.Properties.Refuse = true;
                                force.Properties.Status = true;
                                force.Properties.CensorshipUser = Utils.LoginInfo.GblLoginInfo.sysLoginUserKey;
                                force.Properties.CensorshipTime = SysDateTime.Now;
                                force.Save();

                                e.Cancel = true;
                                return;
                            }
                            else
                            {
                                e.Cancel = true;
                                return;
                            }
                        }
                        else
                        {
                            FrmLog_ForceCanceled_Censorship_Confirm_ confirm = new FrmLog_ForceCanceled_Censorship_Confirm_(force);
                            confirm.ShowDialog();
                            if (Log_TranferData.m_bFlag)
                            {
                                this.grvView.SetFocusedRowCellValue(gclRefuse, true);
                                e.Cancel = true;
                                return;
                            }
                            else
                            {
                                e.Cancel = true;
                                return;
                            }
                        }
                    }
                    else
                    {
                        XtraMessageBox.Show("Phiếu " + m_iLog_ForceCanceledKey + " đã duyệt. Không thể chỉnh sửa");
                        e.Cancel = true;
                        return;
                    }
                }
            }
        }
    }
}
